export const changeHardwareSize = (value: number, from: string, to: string) => {
  if (value === 0) return 0;
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  const fromIndex = sizes.findIndex((item) => item === from);
  const toIndex = sizes.findIndex((item) => item === to);
  if (fromIndex === -1 || toIndex === -1) {
    return value;
  }
  return value / Math.pow(k, toIndex - fromIndex);
};

export const autoFormatHardwareSize = (data: { value: number; unit?: string }) => {
  const { value, unit = 'B' } = data;
  let to = '';
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
  for (let i = 0; i < sizes.length; i++) {
    if (value < Math.pow(k, i + 1)) {
      to = sizes[i];
      break;
    }
  }
  if (!to) to = 'TB';
  const fromIndex = sizes.findIndex((item) => item === unit);
  const toIndex = sizes.findIndex((item) => item === to);
  if (fromIndex === -1 || toIndex === -1) {
    return { value, unit: unit };
  }
  return {
    value: value / Math.pow(k, toIndex - fromIndex),
    unit: to,
  };
};

export const formatMappingFromStrToList = (
  keys: string[],
  separators: string[],
  value?: string,
) => {
  if (!value || separators.length !== 2) return [];
  const values = value
    .split(separators[0])
    .filter((item) => !!item)
    .map((item) => item.split(separators[1]));
  const list: Record<string, string>[] = [];
  values.forEach((v: string[]) => {
    const item = {};
    keys.forEach((k, index) => {
      item[k] = v[index];
    });
    list.push(item);
  });
  return list;
};

export const formatMappingFromListToStr = (
  keys: string[],
  separators: string[],
  value?: Record<string, string>[],
) => {
  if (!value || separators.length !== 2) return '';
  let mappingStr = '';
  value.forEach((v: Record<string, string>) => {
    keys.forEach((k) => {
      mappingStr += v[k] + separators[1];
    });
    mappingStr = mappingStr.slice(0, -1) + separators[0];
  });
  mappingStr = mappingStr.slice(0, -1);
  return mappingStr;
};
